package com.example.a14110100409.yuejiandemo.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.a14110100409.yuejiandemo.R;
import com.example.a14110100409.yuejiandemo.javabean.User;
import com.example.a14110100409.yuejiandemo.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private Button btn_reg,btn_login;
    private EditText et_username,et_password;
    private String error;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }

    public void initViews(){
         /*
        * 1.初始化当前所需的控件，如何初始化
        * findViewById---返回的是一个View的对象所以要强制类型转换成button
        * findViewById是如何查找到对应的id
        * 2.设置button的监听器，通过监听器实现我们点击button要操作的事情
        */
        btn_login = (Button) findViewById(R.id.button);
        btn_reg = (Button) findViewById(R.id.button2);
        et_username = (EditText) findViewById(R.id.editText);
        et_password = (EditText) findViewById(R.id.editText3);

    }
    public void initEvents() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String pwd = et_password.getText().toString();

                UserModel userModel = new UserModel();
                Call<User> call = userModel.login(username,pwd);
                call.enqueue(new Callback<User>() {
                    @Override
                    public void onResponse(Call<User> call, Response<User> response) {
                        String username = et_username.getText().toString();
                        String pwd = et_password.getText().toString();
                        //result = response.body();
                        error = response.body().getError();
                        if("用户名或密码不正确".equals(error)){
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            if(username.equals("") || pwd.equals("")){
                                Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                String userid = response.body().getId().trim();
                                Log.v("获取id",userid);
                                saveUser(userid);
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);                                //传用户Id
                                startActivity(intent);
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    public void saveUser(String user_id){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username",et_username.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id",userid);
        editor.putString("userpass",et_password.getText().toString());
        editor.commit();//提交修改
    }

   /* protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode==RESULT_OK){
            String username = data.getStringExtra("username");
            et_username.setText(username);
        }
        Tencent.onActivityResultData(requestCode, resultCode, data, new BaseUiListener());
        if(requestCode == Constants.REQUEST_API) {
            if(resultCode == Constants.REQUEST_LOGIN) {
                Tencent.handleResultData(data, new BaseUiListener());
            }
        }
    }*/


}
